package com.js.myapp.system.activity;

// Created by JS on 2021/2/6.

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.js.baseview.listview.decoration.LinearLayoutItemDecoration;
import com.js.baseview.listview.listener.EndlessRecyclerOnScrollListener;
import com.js.baseview.selector.entity.SelectionItem;
import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.myapp.R;
import com.js.myapp.system.adapter.SelectionItemLoadMoreAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RefreshLoadMoreActivity extends BaseLoadingDialogActivity {

    @BindView(R.id.tv_list)
    TextView tvList;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private RefreshLoadMoreActivity mContext;

    private SelectionItemLoadMoreAdapter mAdapter;
    private List<SelectionItem> mItems = new ArrayList<>();
    private boolean isLoading = false; // 是否正在加载数据
    private int pageIndex; // 页标
    private int pageSize = 20; // 每页数量
    private int pageSizeForLastTime = 0; // 单次获取的条目数

    public static void start(Context context) {
        Intent intent = new Intent(context, RefreshLoadMoreActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh_load_more);
        ButterKnife.bind(this);
        mContext = this;
        NavigationBarUtil.setSysNavigationColor(this, Color.parseColor("#141d26"));
        StatusBarUtil.setStatusBarColor(this, R.color.color_statusbar_bg);
        initView();
    }

    private void initView() {
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.main_color));
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                mItems.clear();
                getItems();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new LinearLayoutItemDecoration(30));

        mAdapter = new SelectionItemLoadMoreAdapter(mContext, mItems);
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                if (isLoading) {
                    return;
                }
                if (pageSizeForLastTime < pageSize) {
                    return;
                }
                isLoading = true;
                mAdapter.setLoadState(mAdapter.LOADING);
                // 模拟获取网络数据，延时1s
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(() -> getItems());
                    }
                }, 1000);
            }
        });
    }

    private void getItems() {
        swipeRefreshLayout.setRefreshing(true);
        loadItems();
        swipeRefreshLayout.setRefreshing(false);
    }

    private void loadItems() {
        int length = 20;
        if (pageIndex == 5) {
            length = 10;
        }
        List<SelectionItem> list = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            SelectionItem selectionItem = new SelectionItem();
            selectionItem.setId(pageIndex + "" + i);
            selectionItem.setData("这是条目" + pageIndex + "" + i);
            list.add(selectionItem);
        }
        mItems.addAll(list);
        mAdapter.setItems(mItems);
        pageSizeForLastTime = list.size(); // 获取本次获取的条目数
        if (list.size() < pageSize) {
            mAdapter.setLoadState(mAdapter.LOADING_END);
        } else {
            mAdapter.setLoadState(mAdapter.LOADING_COMPLETE);
        }
        pageIndex ++;
        isLoading = false;
    }
}
